﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Business;
using Common;
using QuotationSoftware.DialogForm;

namespace QuotationSoftware
{
    public partial class RegionFrm : Form
    {
        private CreateRegion parentForm;

        public RegionFrm(List<Business.Region> listRegion, CreateRegion _parentForm)
        {
            InitializeComponent();

            Init(listRegion, _parentForm);
        }

        private void Init(List<Business.Region> listRegion, CreateRegion _parentForm)
        {
            this.parentForm = _parentForm;

            List<Business.Region> listRegionNorth = listRegion.Where(p => p.ParentCode == "00001").OrderBy(o=>o.Ordinal).ToList();
            List<Business.Region> listRegionMiddle = listRegion.Where(p => p.ParentCode == "00003").OrderBy(o => o.Ordinal).ToList();
            List<Business.Region> listRegionSouth = listRegion.Where(p => p.ParentCode == "00002").OrderBy(o => o.Ordinal).ToList();
            List<Business.Region> listRegionTayNguyen = listRegion.Where(p => p.ParentCode == "00004").OrderBy(o => o.Ordinal).ToList();

            clb_North.DataSource = listRegionNorth;
            clb_Middle.DataSource = listRegionMiddle;
            clb_South.DataSource = listRegionSouth;
            clb_TayNguyen.DataSource = listRegionTayNguyen;
        }

        // Confirm K00015
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if ((clb_Middle.CheckedItems.Count
                + clb_North.CheckedItems.Count
                + clb_South.CheckedItems.Count
                + clb_TayNguyen.CheckedItems.Count) != 0)
            {
                List<Business.Region> data = new List<Business.Region>();
                data.AddRange(clb_Middle.CheckedItems.Cast<Business.Region>().ToList());
                data.AddRange(clb_North.CheckedItems.Cast<Business.Region>().ToList());
                data.AddRange(clb_South.CheckedItems.Cast<Business.Region>().ToList());
                data.AddRange(clb_TayNguyen.CheckedItems.Cast<Business.Region>().ToList());

                parentForm.ShowCheckedRegions(data);

                this.Close();
            }
            else
            {
                MessageBox.Show(Common.MESSAGE_STRING.NULLCHECKEDREGION_ERROR, Common.MESSAGE_TITLE.ERROR);
            }
        }
    }
}
